using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace XNAStuding
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class dragBall :DrawableGameComponent 
    {
        Vector2 mouseposition = new Vector2();
        public Rectangle posD;
        Rectangle posTex;
        draw d;
        bool mouseOver;//check if the mouse pressed over the object or not
        bool movable;//check if the object can move or not 

        /// <summary>
        ///  this class is used to dragwdrop the textures(tools)in experiment 1 
        /// </summary>
        /// <param name="game"></param>
        /// <param name="d">object from class drawRot which represent the texture(tool) that we want to dragwdrop</param>
        /// <param name="posD">the position of the distination rectangle in which the texture will be drew</param>
        /// <param name="posTex">the position of the source rectangle from which the texture will be drew</param>
        public dragBall(Game game,draw d,Rectangle posD,Rectangle posTex): base(game)
        {
            this.d = d;
            this.posD = posD;
            movable = true;
            this.posTex = posTex;
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            
            base.Initialize();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            
                MouseState mouse_state = Mouse.GetState();
                mouseposition.X = mouse_state.X;
                mouseposition.Y = mouse_state.Y;
                Rectangle mouseRec = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);
                //rectangele that contains the mouse 
                //if the two rectangels intersect each other 
                if (d.posD.Intersects(mouseRec) && mouse_state.LeftButton == ButtonState.Pressed && movable)
                {
                    mouseOver = true;
                }
                //if we pressed over the object 
                if (mouseOver)
                {
                    mouseOver = false;
                    d.posD.X = (int)mouseposition.X - 50;
                    d.posD.Y = (int)mouseposition.Y - 50;

                }
                //if we reached the right postion 
                if (d.posD.X >= posD.X && d.posD.X <= posD.X + 400 && d.posD.Y >= posD.Y && d.posD.Y <= posD.Y + 400 && mouse_state.LeftButton == ButtonState.Released)
                {

                    d.posTex = posTex;
                    d.posD.Width = posTex.Width;
                    d.posD.Height = posTex.Height;
                    d.posD.X = posD.X;
                    d.posD.Y = posD.Y;

                    movable = false;


                }
            
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            d.Draw(gameTime);
            base.Draw(gameTime);

        }
    }
}